home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / netscape / NSRunner.java < prev   
Encoding:
Java Source  |  1997-07-18  |  8.3 KB  |  312 lines

  1. /*
  2.  * @(#)NSRunner.java    1.8 97/05/22
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. package sun.servlet.netscape;
  23.  
  24. import javax.servlet.*;
  25. import java.io.*;
  26. import java.util.Hashtable;
  27. import java.util.Enumeration;
  28. import java.util.Properties;
  29. import sun.servlet.ServletLoader;
  30. import sun.servlet.util.Pool;
  31. import netscape.server.applet.HttpApplet;
  32. import netscape.server.applet.Misconfiguration;
  33. import netscape.net.URLConnection;
  34.  
  35. /**
  36.  * Netscape server application for running servlets.
  37.  *
  38.  * @version    1.8, 05/22/97
  39.  * @author    David Connelly
  40.  */
  41. public
  42. class NSRunner extends HttpApplet implements ServletContext {
  43.     /*
  44.      * The class loaders for all loaded servlets.
  45.      */
  46.     private static Hashtable loaders = new Hashtable();
  47.  
  48.     /*
  49.      * The pool of available request objects.
  50.      */
  51.     private static Pool reqPool = new Pool("sun.servlet.netscape.NSRequest");
  52.  
  53.     /*
  54.      * The pool of available response objects.
  55.      */
  56.     private static Pool resPool = new Pool("sun.servlet.netscape.NSResponse");
  57.  
  58.     /*
  59.      * The class loader for the servlet being invoked.
  60.      */
  61.     private ServletLoader loader;
  62.  
  63.     /*
  64.      * The properties file for servlets.
  65.      */
  66.     private Properties servletProps = new Properties();
  67.  
  68.     /**
  69.      * Constructs a new server application for invoking servlets.
  70.      */
  71.     public NSRunner() {
  72.     }
  73.  
  74.     /**
  75.      * Load servlet properties from servlet prop file
  76.      */
  77.     private void loadServletProps(String servletPropFile) {
  78.  
  79.     File spf = new File(servletPropFile);
  80.     try {
  81.         servletProps.load(new FileInputStream(spf));
  82.     } catch (IOException ioe) {
  83.         log("Could not load servlet properites file "+servletPropFile);
  84.         ioe.printStackTrace();
  85.     }
  86.  
  87.     }
  88.  
  89.     /**
  90.      * Runs the specified servlet.
  91.      */
  92.     public void run() throws Exception {
  93.     // Get servlet directory
  94.     String vpath = getConfigProperty("vpath");
  95.     if (vpath == null) {
  96.         vpath = "/servlet";
  97.     }
  98.     String initfile = getConfigProperty("initfile");
  99.     if (initfile != null) {
  100.         loadServletProps(initfile);
  101.     }
  102.  
  103.     String dir = translateURI(vpath);
  104.     if (dir == null) {
  105.         throw new Misconfiguration("no mapping for " + vpath);
  106.     }
  107.     // Get class loader for directory, and create new loader if not found.
  108.     loader = (ServletLoader)loaders.get(dir);
  109.     if (loader == null) {
  110.         loader = new ServletLoader(dir);
  111.         loaders.put(dir, loader);
  112.     }
  113.     // Get name and path info for servlet being invoked
  114.     String uri = getRequestProperty("uri");
  115.     if (!uri.startsWith(vpath)) {
  116.         throw new Misconfiguration("uri must start with " + vpath);
  117.     }
  118.     int i = vpath.length() + 1;
  119.     if (i > uri.length()) {
  120.         throw new Misconfiguration("no servlet specified");
  121.     }
  122.     int j = uri.indexOf("/", i);
  123.     NSRequest req = (NSRequest)reqPool.alloc();
  124.     req.init(this);
  125.     String name;
  126.     if (j == -1) {
  127.         name = uri.substring(i);
  128.         req.pathInfo = null;
  129.         req.servletPath = uri;
  130.     } else {
  131.         name = uri.substring(i, j);
  132.         req.pathInfo = uri.substring(j);
  133.         req.servletPath = uri.substring(0, j);
  134.     }
  135.     // Get selected servlet and dispatch request
  136.     Servlet s = getServlet(name);
  137.     if (s == null) {
  138.         throw new Misconfiguration("servlet " + name + " not found");
  139.     }
  140.     NSResponse res = (NSResponse)resPool.alloc();
  141.     res.init(this);
  142.     try {
  143.         s.service(req, res);
  144.         } catch (Throwable t) {
  145.         sendDebug(t);
  146.         }
  147.     res.finish();
  148.     // Free up request / response objects after use
  149.     req.reset();
  150.     reqPool.free(req);
  151.     res.reset();
  152.     resPool.free(res);
  153.     }
  154.    
  155.     /**
  156.      * Starts a response for the server application.
  157.      */
  158.     public int startResponse() throws IOException {
  159.     return super.startResponse();
  160.     }
  161.  
  162.     /**
  163.      * Loads a servlet from the current class loader.
  164.      * @param name the Servlet class name
  165.      * @return the Servlet, or null if not found
  166.      * @exception ServletException if an initialization error has occurred
  167.      */
  168.     public Servlet getServlet(String name) throws ServletException {
  169.     // First attempt to resolve the name against the prop file
  170.         String cname = servletProps.getProperty("servlet."+name+".code");
  171.     String init = servletProps.getProperty("servlet."+name+".initArgs");
  172.     try {
  173.         if (cname == null) {
  174.            return loader.loadServlet(name, 
  175.                      new NSServletConfig( this, name, 
  176.                                    init));
  177.             } else {
  178.         return loader.loadServlet(cname, 
  179.                       new NSServletConfig( this, cname, 
  180.                                 init));
  181.         }
  182.     } catch (ServletException e) {
  183.         e.printStackTrace();
  184.         return null;
  185.     }
  186.     }
  187.  
  188.     /**
  189.      * Returns an enumeration of all the currently loaded servlets.
  190.      */
  191.     public Enumeration getServlets() {
  192.     return loader.getServlets();
  193.     }
  194.  
  195.     /**
  196.      * Returns the translated path for the specified virtual path.
  197.      * @param path the virtual path
  198.      * @return the translated path
  199.      */
  200.     public String getRealPath(String path) {
  201.     return translateURI(path);
  202.     }
  203.  
  204.     /**
  205.      * Returns the MIME type for the specified file name.
  206.      * @param name the file name
  207.      * @return the MIME type of the file
  208.      */
  209.     public String getMimeType(String name) {
  210.      String type = URLConnection.guessContentTypeFromName(name);
  211.      return type != null ? type : "text/plain";
  212.     }
  213.  
  214.     /**
  215.      * Returns the name of this server. Unfortunately, there is no
  216.      * NSAPI property to query for this so the name is hardcoded here.
  217.      */
  218.     public String getServerInfo() {
  219.     return "Netscape Enterprise/2.x";
  220.     }
  221.  
  222.     /**
  223.      * Logs a message for a servlet.
  224.      * @param msg the log message
  225.      */
  226.     public void log(String msg) {
  227.         inform(msg);
  228.     }
  229.  
  230.     /**
  231.      * Returns an attribute of the server for the specified key name.
  232.      * @param name the attribute name
  233.      */
  234.     public Object getAttribute(String name) {
  235.     return null;
  236.     }
  237.  
  238.     /*
  239.      * Sends exception stack trace to the client for debugging.
  240.      */
  241.     void sendDebug(Throwable t) throws IOException {
  242.         returnErrorResponse("text/html", SERVER_ERROR);
  243.     PrintStream out = getOutputStream();
  244.     t.printStackTrace(out);
  245.     out.flush();
  246.     }
  247. }
  248.  
  249. /**
  250.  * This class is used to hold internal configuration information about
  251.  * each servlet called by the server.
  252.  */
  253. class NSServletConfig implements ServletConfig {
  254.  
  255.     private Properties servletProperties = new Properties();
  256.     private ServletContext context;
  257.     private String servletName;
  258.  
  259.     NSServletConfig(ServletContext context, String servletName, 
  260.               String propString) {
  261.     this.context = context;
  262.     this.servletName = servletName;
  263.     if (propString != null) {
  264.         propString = propString.replace(',','\n');
  265.         StringBufferInputStream sbin = 
  266.         new StringBufferInputStream(propString);
  267.         String line;
  268.         DataInputStream ds = new DataInputStream(sbin);
  269.         try {
  270.         while ((line = ds.readLine()) != null) {
  271.             int equalTo = line.indexOf("=");
  272.             if (equalTo == -1)
  273.             continue;
  274.             String l = line.substring(0, equalTo);
  275.             String r = line.substring(equalTo + 1, line.length());
  276.             if (l != null && r != null)
  277.             servletProperties.put(l,r);
  278.         }
  279.         } catch (IOException e) {
  280.         System.err.println("Cannot read initial arguments for servlet "
  281.                    + servletName + ": ");
  282.         e.printStackTrace(System.err);
  283.         }
  284.     }
  285.     }
  286.  
  287.     
  288.     /**
  289.      * Returns the servlet context.
  290.      */
  291.     public ServletContext getServletContext() {
  292.     return context;
  293.     }
  294.  
  295.     /**
  296.      * Returns a servlet initialization parameter.
  297.      */
  298.     public String getInitParameter(String name) {
  299.     return (String)servletProperties.get(name);
  300.     }
  301.  
  302.     /**
  303.      * Returns an enumeration of strings representing the initialization
  304.      * parameters for this servlet.
  305.      */
  306.     public Enumeration getInitParameterNames() {
  307.     return servletProperties.propertyNames();
  308.     }
  309.  
  310.     
  311. }
  312.